home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 408_01 / getopt.c < prev    next >
Text File  |  1993-08-03  |  3KB  |  114 lines

  1. /*
  2. **    @(#)getopt.c    2.2 (smail) 1/26/87
  3. */
  4.  
  5. /*
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9.  * directly from AT&T.  The people there assure me that it is indeed
  10.  * in the public domain.
  11.  * 
  12.  * There is no manual page.  That is because the one they gave out at
  13.  * UNIFORUM was slightly different from the current System V Release 2
  14.  * manual page.  The difference apparently involved a note about the
  15.  * famous rules 5 and 6, recommending using white space between an option
  16.  * and its first argument, and not grouping options that have arguments.
  17.  * Getopt itself is currently lenient about both of these things White
  18.  * space is allowed, but not mandatory, and the last option in a group can
  19.  * have an argument.  That particular version of the man page evidently
  20.  * has no official existence, and my source at AT&T did not send a copy.
  21.  * The current SVR2 man page reflects the actual behavor of this getopt.
  22.  * However, I am not about to post a copy of anything licensed by AT&T.
  23.  *
  24.  * 90-09-25  Dan Fandrich <shad04@ccu.umanitoba.ca>
  25.  * Modified for MSDOS under Turbo C ver 2.0
  26.  *     - supports both / and \ as path name separators in ERR()
  27.  *    - lowercases program name in ERR()
  28.  *    - eliminates filename extension in program name in ERR()
  29.  */
  30.  
  31. #ifdef BSD
  32. #include <strings.h>
  33. #else
  34. #include <string.h>
  35. #endif
  36. #include <ctype.h>    /* for tolower() */
  37. #if defined(__MSDOS__) || defined(__OS2__)
  38. #define index strchr
  39. #include <io.h>
  40. #endif
  41. #include "getopt.h"
  42.  
  43. /*LINTLIBRARY*/
  44. #ifndef NULL
  45. #define NULL    ((void *)0)
  46. #endif
  47. #define EOF    (-1)
  48. #define ERR(s, c)    if(opterr){\
  49.     extern int write();\
  50.     char errbuf[2];\
  51.     char *pn,*pp;\
  52.     for (pp = pn = argv[0]; (*pp=tolower(*pp)) != 0; pp++)\
  53.         if ((*pp == '/') || (*pp == '\\')) /* dtf: supports Unix and */\
  54.             pn = pp + 1;           /* MSDOS path names */\
  55.         else if (*pp == '.')\
  56.             *pp = '\0';\
  57.     errbuf[0] = c; errbuf[1] = '\n';\
  58.     (void) write(2, pn, (unsigned)strlen(pn));\
  59.     (void) write(2, s, (unsigned)strlen(s));\
  60.     (void) write(2, errbuf, 2);}
  61.  
  62. extern char *index();
  63.  
  64. int    opterr = 1;
  65. int    optind = 1;
  66. int    optopt;
  67. char    *optarg;
  68.  
  69. int
  70. getopt(argc, argv, opts)
  71. int    argc;
  72. char    **argv, *opts;
  73. {
  74.     static int sp = 1;
  75.     register int c;
  76.     register char *cp;
  77.  
  78.     if(sp == 1)
  79.         if(optind >= argc ||
  80.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  81.             return(EOF);
  82.         else if(strcmp(argv[optind], "--") == 0) {
  83.             optind++;
  84.             return(EOF);
  85.         }
  86.     optopt = c = argv[optind][sp];
  87.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  88.         ERR(": illegal option -- ", c);
  89.         if(argv[optind][++sp] == '\0') {
  90.             optind++;
  91.             sp = 1;
  92.         }
  93.         return('?');
  94.     }
  95.     if(*++cp == ':') {
  96.         if(argv[optind][sp+1] != '\0')
  97.             optarg = &argv[optind++][sp+1];
  98.         else if(++optind >= argc) {
  99.             ERR(": option requires an argument -- ", c);
  100.             sp = 1;
  101.             return('?');
  102.         } else
  103.             optarg = argv[optind++];
  104.         sp = 1;
  105.     } else {
  106.         if(argv[optind][++sp] == '\0') {
  107.             sp = 1;
  108.             optind++;
  109.         }
  110.         optarg = NULL;
  111.     }
  112.     return(c);
  113. }
  114.